'use strict';
const Generator = require('yeoman-generator');
const chalk = require('chalk');
const yosay = require('yosay');
const question = require('./question');
const doThing = require('./doThing');
const defaultConfig = require('./config/gen');

module.exports = class extends Generator {
  
  prompting() {

    this.log(yosay(
       chalk.red('生成java实体类') +"从mysql中获取表结构"
    ));
    this.log(`
        1. 确保数据库的表名和列名下划线命名
        2. 只用于mysql
        3. 请确保你已经提交代码，否则将造成不可挽回的后果
      `);


    const prompts = question;
    return this.prompt([{
      type: 'list',
      name: 'answer',
      message: '这个世界上谁最帅?',
      choices: [{
        value: '1',
        name: '刘德华',
        checked: true
      },
      {
        value: '1',
        name: '古天乐',
        checked: false
      },
      {
        value: '2',
        name: '刘老师',
        checked: false
      }
    
      ]
    }]).then(props=>{
      if(props.answer<2){
        this.log( chalk.blue("审美有问题！"));
        throw "请选择刘老师";
      }else{
        var cf = this.config.getAll();
        if(Object.keys(cf).length>0){//配置文件不为空
          global.props=cf;
          return cf;
        }
        return this.prompt(prompts).then(props => {
          // 配置文件为空
          var i=null;
          if(!question.every(item=>!item.require||props[(i=item).name])){//检查必须的配置项
            throw i.message+"不能为空";
          }
          Object.keys(props).filter(p=>typeof(props[p])=="string").forEach(p=>props[p]=props[p].trim());
          global.props = props = Object.assign({},defaultConfig,props);//混合全局配置项

          Object.keys(props).forEach(item=>{//写入配置文件
            this.config.set(item,props[item]);
          });
          return props;
        });
      }
    });

    
  }

  writing() {
    doThing(this);
    // this.fs.copy(
    //   this.templatePath('dummyfile.txt'),
    //   this.destinationPath('dummyfile.txt')
    // );
  }

  install() {
    this.installDependencies();
  }
};
